package com.xuanyuan.domain;

import com.xuanyuan.enums.ExceptionEnum;
import org.springframework.util.StringUtils;

import java.util.HashMap;

public class AjaxResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    public static final String CODE_TAG = "code";
    public static final String MSG_TAG = "msg";
    public static final String DATA_TAG = "data";

    public AjaxResult() {
    }

    public AjaxResult(int code, String msg) {
        super.put("code", code);
        super.put("msg", msg);
    }

    public AjaxResult(int code, String msg, Object data) {
        super.put("code", code);
        super.put("msg", msg);
        if (!StringUtils.isEmpty(data)) {
            super.put("data", data);
        }

    }

    public AjaxResult(String code, String msg, Object data) {
        super.put("code", code);
        super.put("msg", msg);
        if (!StringUtils.isEmpty(data)) {
            super.put("data", data);
        }

    }

    public static AjaxResult toAjax(boolean result) {
        return result ? success() : error();
    }

    public static AjaxResult toAjax(int rows) {
        return rows > 0 ? success() : error();
    }

    public static AjaxResult success() {
        return success("操作成功");
    }


    public static AjaxResult success(Object data) {
        return success("操作成功", data);
    }

    public static AjaxResult success(String msg) {
        return success(msg, "");
    }

    public static AjaxResult success(String msg, Object data) {
        return new AjaxResult(200, msg, data);
    }

    public static AjaxResult error() {
        return error("操作失败");
    }

    public static AjaxResult error(String msg) {
        return error(msg, "");
    }

    public static AjaxResult error(String msg, Object data) {
        return new AjaxResult(-1, msg, data);
    }

    public static AjaxResult error(int code, String msg) {
        return new AjaxResult(code, msg, (Object) null);
    }

    public static AjaxResult error(ExceptionEnum e) {
        return error(e.getCode(), e.getMsg());
    }

    public static enum Type {
        SUCCESS(200),
        WARN(301),
        ERROR(500);

        private final int value;

        private Type(int value) {
            this.value = value;
        }

        public int value() {
            return this.value;
        }
    }
}

